package com.intco.ait.quotation.api.infrastructure.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intco.ait.quotation.api.common.model.pojo.MaterialBaseData;
import com.intco.ait.quotation.api.common.model.request.MaterialSearchVo;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.InventoryMoulding;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.Material;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.ProductAttribute;
import com.intco.ait.quotation.api.infrastructure.mybatis.mapper.EncodingPlatformMapper;
import com.intco.ait.quotation.api.infrastructure.mybatis.mapper.SdMrpMapper;
import com.intco.ait.quotation.api.infrastructure.mybatis.mapper.ShMrpMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class EncodingPlatformService {

    @Autowired
    private EncodingPlatformMapper mapper;

    @Autowired
    private ShMrpMapper shMrpMapper;

    @Autowired
    private SdMrpMapper sdMrpMapper;

    public IPage<Material> search(MaterialSearchVo search) {
        return mapper.getMaterialList(new Page<>(search.getCurrent(), search.getSize()),
                search.getProductBusiness().toLowerCase(),
                search.getMaterialCode(),
                "",
                "",
                "",
                "",
                search.getBuildingType(),
                search.getOutsource()
        );
    }

    public Material getProductAttributeByMaterialCode(String materialCode) {
        Material material = mapper.getMaterialByCode(materialCode);
        try {
            ProductAttribute productAttribute = mapper.getProductAttributeByMaterialCode(materialCode);
            material.setOutsource(productAttribute.getOutsource() == 1);
            material.setMaterialShape(productAttribute.getProductShape());
            material.setSacCode(productAttribute.getSacCode());
            material.setLathStyle(productAttribute.getLathStyle());
            material.setSupplierProductModel(productAttribute.getSupplierProductModel());
            material.setMdfIsNotFsc(productAttribute.getFsc());
            material.setCompany(productAttribute.getCompany());
            material.setNetWeight(productAttribute.getNetWeight());
            material.setGrossWeight(productAttribute.getGrossWeight());
            material.setPieceVolume(productAttribute.getPieceVolume());
            material.setBoxLength(productAttribute.getBoxLength());
            material.setBoxWidth(productAttribute.getBoxWidth());
            material.setBoxHeight(productAttribute.getBoxHeight());
            material.setBoxNetWeight(productAttribute.getBoxNetWeight());
            material.setBoxGrossWeight(productAttribute.getBoxGrossWeight());
            material.setBoxVolume(productAttribute.getBoxVolume());
            material.setPrePcs1(productAttribute.getPrePcs1());
            material.setPrePcs2(productAttribute.getPrePcs2());
            material.setPreLength1(productAttribute.getPreLength1());
            material.setPreLength2(productAttribute.getPreLength2());
            material.setPreAngle1(productAttribute.getPreAngle1());
            material.setPreAngle2(productAttribute.getPreAngle2());
            material.setPkMeasdoc(productAttribute.getPkMeasdoc());
            material.setBuildingFinishedShape(productAttribute.getBuildingFinishedShape());
            material.setMouldBase(productAttribute.getMouldBase());
            material.setMouldBaseImg(productAttribute.getMouldBaseImg());
        } catch (Exception e) {
            log.error("获取物料属性失败，物料编码：{}", materialCode, e);
        }
        List<InventoryMoulding> list;
        if (material.getCompany().equals("SD")) {
            list = sdMrpMapper.getListByInvCode(materialCode);
        } else {
            list = shMrpMapper.getListByInvCode(materialCode);
        }
        if (!list.isEmpty()) {
            MaterialBaseData baseData = new MaterialBaseData();
            MaterialBaseData.MouldData mouldData = baseData.getFinished().getMouldData();
            Optional<MaterialBaseData.ValueName> optional;
            for (InventoryMoulding moulding : list) {
                optional = mouldData.getCuttingModeList().stream().filter(t -> t.getValue().equals(moulding.getCuttingMode())).findFirst();
                optional.ifPresent(keyValueVo -> moulding.setCuttingModeName(keyValueVo.getName()));
                optional = baseData.getFinished().getCurrency().getDeclareFlagList().stream().filter(t -> t.getValue().equals(moulding.getDeclareFlag())).findFirst();
                optional.ifPresent(keyValueVo -> moulding.setDeclareFlagName(keyValueVo.getName()));
            }
            //防止因新老数据或者测试数据引起的多外框
            String outerFrameMouldType = "外框";
            if (list.stream().filter(t -> t.getMouldType().equals(outerFrameMouldType)).count() > 1) {
                Optional<InventoryMoulding> optional1 = list.stream().filter(t -> t.getMouldType().equals(outerFrameMouldType)).findFirst();
                optional1.ifPresent(list::remove);
            }
        }
        material.setItemList(list);
        return material;
    }


}
